"""
@FileName：all_citys.py.py\n
@Description：\n
@Author：lssyyr\n
@Time：2024/12/8 23:10\n
"""
import requests
import re
import os
if not os.path.exists('../images'):
    os.mkdir('../images')
url = 'https://movie.douban.com/top250'
headers ={
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
}
for page in range(0,226,25):
    params = {
        'start': str(page),
        'filter':''
    }
    try:
        response = requests.get(url, params=params, headers=headers,data=params).text
    except requests.RequestException as e:
        print(f"请求失败: {e}")
        continue

    pattern = r'<img\s+[^>]*alt="([^"]*)"\s+[^>]*src="([^"]*)"[^>]*>'
    matches = re.findall(pattern, response)
    for alt, src in matches:
        try:
            img_data = requests.get(src, headers=headers).content
            img_nameold = src.split('/')[-1]
            img_gs = img_nameold.split('.')[-1]
            img_name = alt + '.' + img_gs
            img_path = './images/' + img_name
            with open(img_path, 'wb') as fp:
                fp.write(img_data)
                print(img_name, '下载成功')
        except requests.RequestException as e:
            print(f"下载图片失败: {e}")
